
package com.wyzz.hr.action;

import java.util.*;
import org.apache.commons.lang.StringUtils;

import com.wyzz.hr.base.BaseAction;
import com.wyzz.hr.base.ComonBeans;
import com.wyzz.hr.bo.IDepartmentBO;
import com.wyzz.hr.bo.IJobTitleBo;
import com.wyzz.hr.bo.IOuperfcriteriaBo;
import com.wyzz.hr.bo.IOuqualifyBo;
import com.wyzz.hr.bo.IOuresponseBo;
import com.wyzz.hr.bo.IPositionBaseBo;
import com.wyzz.hr.bo.ISysLogBO;
import com.wyzz.hr.pojo.Department;
import com.wyzz.hr.pojo.Ouperfcriteria;
import com.wyzz.hr.pojo.Ouqualify;
import com.wyzz.hr.pojo.Ouresponse;
import com.wyzz.hr.pojo.PositionBase;
import com.wyzz.hr.spring.SpringBeanFactory;
import com.wyzz.hr.util.DatabaseSysConfigManager;
import com.wyzz.hr.util.SysConfigManager;

public class pbManageAction extends BaseAction {

	public pbManageAction() {
		jobtitleList = null;
		lastOperate = null;
		pcTypeMap = ComonBeans
				.getValuesToMap("pcType", new boolean[] { false });
		pb = new PositionBase();
		pbSelectList = new ArrayList();
		dbConfigManager = DatabaseSysConfigManager.getInstance();
	}

	public String execute() throws Exception {
		return "success";
	}

	public String pbBasicInfo() {
		Map dbMap = dbConfigManager.getProperties();
		exceedConfig = (String) dbMap.get("sys.position.max.exceed");
		if (exceedConfig == null)
			exceedConfig = "0";
		IJobTitleBo jobTitleBo = (IJobTitleBo) SpringBeanFactory
				.getBean("jobTitleBo");
		jobtitleList = jobTitleBo.FindAllJobTitle();
		IPositionBaseBo pbBo = (IPositionBaseBo) SpringBeanFactory
				.getBean("positionBaseBo");
		String fetch[] = { PositionBase.PROP_PB_DEPT_ID,
				PositionBase.PROP_PB_SUP_ID };
		if (pbId == null) {
			if (StringUtils.isEmpty(deptId)
					&& StringUtils.isNotEmpty(parentPbId)) {
				PositionBase parentPb = pbBo.loadPb(parentPbId, fetch);
				deptId = parentPb.getPbDeptId().getId();
				pb.setPbDeptId(parentPb.getPbDeptId());
				pb.setPbSupId(parentPb);
			} else if (StringUtils.isEmpty(parentPbId)
					&& StringUtils.isNotEmpty(deptId)) {
				IDepartmentBO deptBo = (IDepartmentBO) SpringBeanFactory
						.getBean("departmentBO");
				Department dept = deptBo.getDeptById(deptId);
				pb.setPbDeptId(dept);
			}
			lastOperate = null;
			initSelectPbList();
			return "success";
		} else {
			pb = pbBo.loadPb(pbId, fetch);
			pbBo.setPBEmpInfo(pb);
			initSelectPbList();
			return "success";
		}
	}

	private void initSelectPbList() {
		IPositionBaseBo pbBo = (IPositionBaseBo) SpringBeanFactory
				.getBean("positionBaseBo");
		if (pb.getId() == null)
			pbSelectList = pbBo.getActivePbsByDept(pb.getPbDeptId().getId());
		else if (pb.getPbInCharge().intValue() == 1) {
			pbSelectList = pbBo.getSupActivePbsByDept(pb.getPbDeptId().getId());
		} else {
			pbSelectList = pbBo.getActivePbsByDept(pb.getPbDeptId().getId());
			Iterator pbIt = pbSelectList.iterator();
			do {
				if (!pbIt.hasNext())
					break;
				PositionBase pbIn = (PositionBase) pbIt.next();
				if (pbIn.getId().equals(pb.getId()))
					pbIt.remove();
			} while (true);
		}
	}

	public String checkPbName(String id, String pbName, String deptId) {
		IPositionBaseBo pbBo = (IPositionBaseBo) SpringBeanFactory
				.getBean("positionBaseBo");
		if (pbBo.duplicateName(id, pbName, deptId))
			return "FAIL";
		else
			return "SUCC";
	}

	public String savePbBasicInfo() {
		IPositionBaseBo pbBo = (IPositionBaseBo) SpringBeanFactory
				.getBean("positionBaseBo");
		initPb(pb);
		String operatorNo = getCurrentEmpNo();
		if (pb.getPbInCharge().intValue() == 1 && pb.getPbMaxEmployee() != null
				&& pb.getPbMaxEmployee().intValue() > 1) {
			pbId = pb.getId();
			return "error";
		} else {
			pbBo.saveOrupdatePb(pb, operatorNo);
			pbId = pb.getId();
			return "success";
		}
	}

	private void initPb(PositionBase pb) {
		IPositionBaseBo pbBo = (IPositionBaseBo) SpringBeanFactory
				.getBean("positionBaseBo");
		SysConfigManager dbConfigManager = DatabaseSysConfigManager
				.getInstance();
		Map dbMap = dbConfigManager.getProperties();
		String exceed = (String) dbMap.get("sys.position.max.exceed");
		if (exceed == null)
			exceed = "1";
		if (StringUtils.isEmpty(pb.getId())) {
			lastOperate = "new";
			pb.setId(null);
			pb.setPbMaxExceed(Integer.valueOf(Integer.parseInt(exceed)));
			pb.setPbStatus(Integer.valueOf(1));
			pb.setPbInCharge(Integer.valueOf(0));
			pb
					.setPbSortId(pbBo.getNextPbSortIdOfDept(pb.getPbDeptId()
							.getId()));
		} else {
			lastOperate = "modify";
			PositionBase oldPb = pbBo.loadPb(pb.getId(), null);
			if (oldPb.getPbMaxEmployee() == null) {
				if (pb.getPbMaxEmployee() != null) {
					pb.setMaxEmpChanged(Boolean.valueOf(true));
					pb.setHistMaxEmp(oldPb.getPbMaxEmployee());
				}
			} else if (!oldPb.getPbMaxEmployee().equals(pb.getPbMaxEmployee())) {
				pb.setMaxEmpChanged(Boolean.valueOf(true));
				pb.setHistMaxEmp(oldPb.getPbMaxEmployee());
			}
			pb.setPbMaxExceed(oldPb.getPbMaxExceed());
			pb.setPbStatus(oldPb.getPbStatus());
			pb.setPbInCharge(oldPb.getPbInCharge());
			pb.setPbSortId(oldPb.getPbSortId());
		}
		if (pb.getPbSupId() == null
				|| StringUtils.isEmpty(pb.getPbSupId().getId()))
			pb.setPbSupId(null);
	}

	public String showPbTable() {
		IPositionBaseBo pbBo = (IPositionBaseBo) SpringBeanFactory
				.getBean("positionBaseBo");
		pbSelectList = pbBo.getAllPbsOfDept(new String[] { deptId });
		for (int i = 0; i < pbSelectList.size(); i++) {
			PositionBase pb = (PositionBase) pbSelectList.get(i);
			pbBo.setPBEmpInfo(pb);
		}

		return "success";
	}

	public String savePbOrder(String ids[]) {
		IPositionBaseBo pbBo = (IPositionBaseBo) SpringBeanFactory
				.getBean("positionBaseBo");
		if (pbBo.savePbOrder(ids))
			return "SUCC";
		else
			return "FAIL";
	}

	public String showPbHistAction() {
		ISysLogBO logBO = (ISysLogBO) SpringBeanFactory.getBean("logBO");
		pbHistActionList = logBO.getLogs("positionbase", pbId.trim());
		return "success";
	}

	public String pbResponse() {
		IOuresponseBo pbRespBo = (IOuresponseBo) SpringBeanFactory
				.getBean("ouresponseBo");
		pbRespList = pbRespBo.getPbRespByPbId(pbId);
		return "success";
	}

	public String addPbResp(Ouresponse pbResp) {
		IOuresponseBo pbRespBo = (IOuresponseBo) SpringBeanFactory
				.getBean("ouresponseBo");
		if (pbRespBo.addPbResp(pbResp))
			return pbResp.getId();
		else
			return "EXISTED";
	}

	public String updatePbResp(Ouresponse pbResp) {
		IOuresponseBo pbRespBo = (IOuresponseBo) SpringBeanFactory
				.getBean("ouresponseBo");
		if (pbRespBo.updatePbResp(pbResp))
			return "SUCC";
		else
			return "EXISTED";
	}

	public String delPbResp(String respId) {
		IOuresponseBo pbRespBo = (IOuresponseBo) SpringBeanFactory
				.getBean("ouresponseBo");
		if (pbRespBo.delPbResp(respId))
			return "SUCC";
		else
			return "FAIL";
	}

	public String savePbRespOrder(String ids[]) {
		IOuresponseBo pbRespBo = (IOuresponseBo) SpringBeanFactory
				.getBean("ouresponseBo");
		pbRespBo.savePbRespOrder(ids);
		return "SUCC";
	}

	public String pbPerfcr() {
		IOuperfcriteriaBo perfcrBo = (IOuperfcriteriaBo) SpringBeanFactory
				.getBean("ouperfcriteriaBo");
		pbPerfcrList = perfcrBo.getPbPerfcriteria(pbId);
		return "success";
	}

	public String addPbPerfcr(Ouperfcriteria pbPerfcr) {
		IOuperfcriteriaBo perfcrBo = (IOuperfcriteriaBo) SpringBeanFactory
				.getBean("ouperfcriteriaBo");
		if (perfcrBo.addPbPerfcr(pbPerfcr))
			return pbPerfcr.getId();
		else
			return "EXISTED";
	}

	public String updatePbPerfcr(Ouperfcriteria pbPerfcr) {
		IOuperfcriteriaBo perfcrBo = (IOuperfcriteriaBo) SpringBeanFactory
				.getBean("ouperfcriteriaBo");
		if (perfcrBo.updatePbPerfcr(pbPerfcr))
			return "SUCC";
		else
			return "EXISTED";
	}

	public String delPbPerfcr(String id) {
		IOuperfcriteriaBo perfcrBo = (IOuperfcriteriaBo) SpringBeanFactory
				.getBean("ouperfcriteriaBo");
		if (perfcrBo.delPbPerfcr(id))
			return "SUCC";
		else
			return "FAIL";
	}

	public String savePbPerfcrOrder(String ids[]) {
		IOuperfcriteriaBo perfcrBo = (IOuperfcriteriaBo) SpringBeanFactory
				.getBean("ouperfcriteriaBo");
		perfcrBo.savePbPerfcrOrder(ids);
		return "SUCC";
	}

	public String pbQualify() {
		IOuqualifyBo pbQualifyBo = (IOuqualifyBo) SpringBeanFactory
				.getBean("ouqualifyBo");
		pbQualifyList = pbQualifyBo.getpbQualifyByPbId(pbId);
		return "success";
	}

	public String addPbQualify(Ouqualify pbQualify) {
		IOuqualifyBo pbQualifyBo = (IOuqualifyBo) SpringBeanFactory
				.getBean("ouqualifyBo");
		if (pbQualifyBo.addPbQualify(pbQualify))
			return pbQualify.getId();
		else
			return "EXISTED";
	}

	public String updatePbQualify(Ouqualify pbQualify) {
		IOuqualifyBo pbQualifyBo = (IOuqualifyBo) SpringBeanFactory
				.getBean("ouqualifyBo");
		if (pbQualifyBo.updatePbQualify(pbQualify))
			return "SUCC";
		else
			return "EXISTED";
	}

	public String delPbQualify(String id) {
		IOuqualifyBo pbQualifyBo = (IOuqualifyBo) SpringBeanFactory
				.getBean("ouqualifyBo");
		if (pbQualifyBo.delPbQualify(id))
			return "SUCC";
		else
			return "FAIL";
	}

	public String savePbQualifyOrder(String ids[]) {
		IOuqualifyBo pbQualifyBo = (IOuqualifyBo) SpringBeanFactory
				.getBean("ouqualifyBo");
		pbQualifyBo.savePbQualifyOrder(ids);
		return "SUCC";
	}

	public String convertPbStatus(String id) {
		IPositionBaseBo pbBo = (IPositionBaseBo) SpringBeanFactory
				.getBean("positionBaseBo");
		String operatorNo = getCurrentEmpNo();
		PositionBase pb = pbBo.loadPb(id, null);
		Integer status = pb.getPbStatus();
		if (status.intValue() == 1)
			if (pbBo.disablePb(id).equals("SUCC"))
				return "0";
			else
				return "FAIL";
		if (pbBo.startPb(id).equals("SUCC"))
			return "1";
		else
			return "FAIL";
	}

	public String delPb(String id) {
		IPositionBaseBo pbBo = (IPositionBaseBo) SpringBeanFactory
				.getBean("positionBaseBo");
		PositionBase pb = pbBo.loadPb(id, null);
		if (pb == null)
			return "\u804C\u4F4D\u4E0D\u5B58\u5728\uFF01";
		if (pb.getPbInCharge().intValue() == 1) {
			return "\u8D1F\u8D23\u804C\u4F4D\u4E0D\u80FD\u5220\u9664\uFF01";
		} else {
			String errorMsg = pbBo.delPb(id);
			return errorMsg;
		}
	}

	public String getPbId() {
		return pbId;
	}

	public void setPbId(String pbId) {
		this.pbId = pbId;
	}

	public String getDeptId() {
		return deptId;
	}

	public void setDeptId(String deptId) {
		this.deptId = deptId;
	}

	public PositionBase getPb() {
		return pb;
	}

	public void setPb(PositionBase pb) {
		this.pb = pb;
	}

	public String getParentPbId() {
		return parentPbId;
	}

	public void setParentPbId(String parentPbId) {
		this.parentPbId = parentPbId;
	}

	public List getPbSelectList() {
		return pbSelectList;
	}

	public void setPbSelectList(List pbSelectList) {
		this.pbSelectList = pbSelectList;
	}

	public String getExceedConfig() {
		return exceedConfig;
	}

	public void setExceedConfig(String exceedConfig) {
		this.exceedConfig = exceedConfig;
	}

	public List getPbRespList() {
		return pbRespList;
	}

	public void setPbRespList(List pbRespList) {
		this.pbRespList = pbRespList;
	}

	public Map getPcTypeMap() {
		return pcTypeMap;
	}

	public void setPcTypeMap(Map pcTypeMap) {
		this.pcTypeMap = pcTypeMap;
	}

	public List getPbQualifyList() {
		return pbQualifyList;
	}

	public void setPbQualifyList(List pbQualifyList) {
		this.pbQualifyList = pbQualifyList;
	}

	public List getPbHistActionList() {
		return pbHistActionList;
	}

	public void setPbHistActionList(List pbHistActionList) {
		this.pbHistActionList = pbHistActionList;
	}

	public String getLastOperate() {
		return lastOperate;
	}

	public void setLastOperate(String lastOperate) {
		this.lastOperate = lastOperate;
	}

	public List getJobtitleList() {
		return jobtitleList;
	}

	public void setJobtitleList(List jobtitleList) {
		this.jobtitleList = jobtitleList;
	}

	public List getPbPerfcrList() {
		return pbPerfcrList;
	}

	public void setPbPerfcrList(List pbPerfcrList) {
		this.pbPerfcrList = pbPerfcrList;
	}

	private static final long serialVersionUID = 1L;
	private String pbId;
	private List jobtitleList;
	private String deptId;
	private String parentPbId;
	private String exceedConfig;
	private String lastOperate;
	private Map pcTypeMap;
	private PositionBase pb;
	private List pbRespList;
	private List pbQualifyList;
	private List pbPerfcrList;
	private List pbHistActionList;
	private List pbSelectList;
	private SysConfigManager dbConfigManager;
}
